/**
 * jQuery.loader Plugin
 * @author Stéphan Zych <info@monkeymonk.be>
 * @copyriht 2010-2011 Stéphan Zych <info@monkeymonk.be>
 * @license New BSD License <http://creativecommons.org/licenses/BSD/>
 */

var TYPE_LOADING = "Loading";
var TYPE_SAVING = "Saving";
var TYPE_DELETING = "Deleting";
var typeTimer;
var minLoaderTime = 1000;
var isToClose = false;
var timeCounter = 0;
var timerTimeLimit = 300;
var callBackFunction;

function showLoader(isToShow, type, method) {
	//"maySaving" "myLoading"
	//console.log("----> In Loading:", callBackFunction);
	var originalText = type;
	isToClose = isToShow;
 	callBackFunction = method;
	
	if (isToShow) {
		i = 0;
		$('body').addClass('loading').loader('show', {
			myType : type,
			tpl : '<div class="{className} hide"><div class="{className}-load"><div class="loaderContent"></div></div><div class="{className}-overlay"></div></div>',
			loader : false,
			delay : 0,
			overlay : true
		});
		$('.loaderContent').text(type);
		/////////////////////////////////////////////////////////////////
		//var originalText = $('.loaderContent').text(), i = 0;

		typeTimer = setInterval(function() {

			timeCounter += timerTimeLimit;

			if ((timeCounter > minLoaderTime) && !isToClose) {
				//console.log("callBackFunction",callBackFunction);
				clearInterval(typeTimer);
				//console.log("typeTimer after:", typeTimer);
				$('body').removeClass('loading').loader('hide');
				timeCounter = 0;
				if (callBackFunction != undefined) {
					//console.log("callBackFunction",callBackFunction);
					callBackFunction();
				}
				return;
			}

			$('.loaderContent').append(".");
			i++; 

			if (i == 4) {
				//console.log("type:",type);
				$('.loaderContent').text(type);
				i = 0;
			}

		}, timerTimeLimit);
		/////////////////////////////////////////////////////////////////
		/**
		 * Replace the Description Text
		 */
		//
	}
	else if (timeCounter > minLoaderTime) {
			//console.log("callBackFunction",callBackFunction);
		//hide Loader
		//console.log("typeTimer before:", typeTimer);
		clearInterval(typeTimer);
		//console.log("typeTimer after:", typeTimer);
		if (callBackFunction != undefined) {
			callBackFunction();
		}
		$('body').removeClass('loading').loader('hide');
	}
}( function($) {"use strict";

		var render = function(template, data) {
			for (var k in data) {
				template = template.replace(new RegExp('{' + k + '}', 'g'), data[k]);
			}

			return template;
		};
		// render

		var Loader = function() {
			var defaults = {
				className : 'loader',

				tpl : '<div class="{className} hide"><div class="{className}-load"></div><div class="{className}-overlay"></div></div>',

				delay : 200,
				loader : true,
				overlay : true,

				onHide : function() {
				},
				onShow : function() {
				}
			};
			// defaults

			var init = function(oParams) {
				return this.each(function() {
					defaults = $.extend({}, defaults, oParams);

					var $self = $(this), $loader;

					$self.append(render(defaults.tpl, defaults));
					$loader = $('> .' + defaults.className, $self);

					if (defaults.overlay) {
						$loader.addClass('overlay');
					}

					if (defaults.loader) {
						$('.' + defaults.className + '-load', $loader).on('click', function() {
							$self.loader('hide', defaults.onHide);
						});
					}
				});
			};
			// init

			var show = function(oParams) {
				defaults = $.extend({}, defaults, oParams);

				var self = this, $self = $(self), $loader = $('> .' + defaults.className + ':first', $self);

				if (!$loader.length) {
					init.call(self, oParams);
					$loader = $('> .' + defaults.className + ':first', $self);
				}

				$loader.fadeIn(defaults.delay, function() {
					$loader.removeClass('hide');

					defaults.onShow(defaults);
				});
			};
			// show

			var hide = function(onHide) {
				defaults = $.extend({}, defaults, {
					onHide : onHide
				});

				var self = this, $self = $(self), $loader = $('> .' + defaults.className + ':first', $self);

				if ($loader.length) {
					$loader.fadeOut(defaults.delay, function() {
						$loader.addClass('hide');

						defaults.onHide(defaults);
					});
				}
			};
			// hide

			return {
				init : init,
				show : show,
				hide : hide
			};
		};
		// Loader

		$.fn.loader = function(options) {
			if (!$.data(this, 'loader')) {
				$.data(this, 'loader', new Loader(this, options));
			}

			var plugin = $.data(this, 'loader');

			if (plugin[options]) {
				return plugin[options].apply(this, Array.prototype.slice.call(arguments, 1));
			} else if ( typeof options === 'object' || !options) {
				return plugin.init.apply(this, arguments);
			} else {
				$.error('Method "' + arguments[0] + '" does not exist in $.loader plugin!');
			}
		};
		// $.fn.loader

	}(jQuery));
// jQuery.loader() by Stéphan Zych (monkeymonk.be)